package com.shuyangyang.algorithm;

import java.util.Arrays;

/**
 * 选择排序算法学习
 * 1、从头往后依次找出最小值
 * 2、记录最小值的位置
 * 3、将起始位置的值与最小值位置交换
 *
 * @author shuyangyang
 * @version 1.0.0
 * @date 2020-3-23 11:54:45
 */
public class SelectionSort {

    public static void main(String[] args) {

        int[] array = {2, 6, 1, 9, 5, 7, 3, 8, 4};

        for (int j = 0; j < array.length - 1; j++) {
            int minPos = j;

            for (int i = j + 1; i < array.length; i++) {
                minPos = array[i] < array[minPos] ? i : minPos;
            }

            swap(array, j, minPos);

            System.out.print("第" + (j + 1) + "次排序数组结果：");
            System.out.println(Arrays.toString(array));
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
